//
// Created by dong on 2025/10/22.
//

#include "ShapeBar.h"

ShapeBar::ShapeBar()
{
    SDL_Rect temp = {40,30,40,40};
    vector<string> shapes = {"Line","Rect","Ellipse","Triangle"};
    for (int i=0;i<4;i++)
    {
        temp.x+=temp.w;
        m_shapesBar.push_back(Button(temp,shapes[i],{250,251,252}));
    }
    isClicked = 0;
}

bool ShapeBar::mousePressEvent(SDL_Event* msg)
{
    for (int i=0;i<m_shapesBar.size();i++)
    {
        if (m_shapesBar[i].mousePressEvent(msg)==true)
        {
            isClicked=i;
            return true;
        }
    }
    return  false;
}

void ShapeBar::render(SDL_Renderer* render)
{
    for (auto v:m_shapesBar)
    {
        SDL_Rect dst;
        v.render(render,Button::ColorTool);
        string type = v.getName();
        dst = v.getRect();
        SDL_SetRenderDrawColor(render,0,0,0,0);
        if (type=="Line")
        {
            int rx = dst.w-INTERVAL*4 +dst.x;
            int ry = dst.h-INTERVAL*4 +dst.y;
            SDL_RenderDrawLine(render,dst.x+INTERVAL* 2,dst.y+INTERVAL*2,rx,ry);
        }else if (type=="Rect")
        {
            SDL_Rect temp = {dst.x+INTERVAL* 2,dst.y+INTERVAL*2,dst.w-INTERVAL*4,dst.h-INTERVAL*4};
            SDL_RenderDrawRect(render,&temp);
        }else if (type =="Ellipse")
        {
            SDL_Rect temp = {dst.x+INTERVAL* 2,dst.y+INTERVAL*2,dst.w-INTERVAL*4,dst.h-INTERVAL*4};
            SDL_RenderDrawEllipse(render,&temp);
        }else if (type=="Triangle")
        {
            SDL_Rect temp = {dst.x+INTERVAL* 2,dst.y+INTERVAL*2,dst.w-INTERVAL*4,dst.h-INTERVAL*4};
            SDL_RenderDrawTriangle(render,&temp);
        }
    }
}

string ShapeBar::name() const
{
    return m_shapesBar[isClicked].getName();
}
